//
// Created by redcat1 on 01.05.2024.
//

#include "vector_.h"
#include <sstream>
#include <iomanip>

//
Vector::Vector(int size_, double value_)
  : size(size_)
  , data()
{
  for (int i = 0; i < size_; i++)
  {
    data[i] = value_;
  }
}

//
Vector::Vector(const Vector &vector)
  : size(vector.size)
  , data()
{
  for (int i = 0; i < size; i++)
  {
    data[i] = vector.data[i];
  }
}

//
Vector& Vector::operator=(const Vector &vector)
{
  if (this != &vector)
  {
    size = vector.size;
    for (int i = 0; i < size; i++)
    {
      data[i] = vector.data[i];
    }
  }
  return *this;
}

//
double Vector::operator[](int index) const
{
  return data[index];
}

//
double& Vector::operator[](int index)
{
  return data[index];
}

//
int Vector::Size() const
{
  return size;
}

//
double Vector::Erase(int index_)
{
  double value = data[index_];
  for (int i = index_; i < size - 1; i++)
  {
    std::swap(data[i], data[i + 1]);
  }
  --size;
  return value;
}

//
void Vector::Insert(int index_, double value_)
{
  data[size] = value_;
  size++;
  for (int i = size - 1; i > index_; i--)
  {
    std::swap(data[i], data[i - 1]);
  }
}

//
std::string Vector::ToString() const
{
  std::stringstream ss;
  ss << std::setprecision(4) << "{";
  for (int i = 0; i < size; ++i)
  {
    ss << data[i];
    if (i != size - 1)
    {
      ss << ", ";
    }
  }
  ss << "}";
  return ss.str();
}
